﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Central.Linq.Mvc {

    public static class Extensions {

        public static IEnumerable<FieldDefinition> ToFieldDefinition(this IQueryable<FieldInformation> source) {
            if (source == null) throw new ArgumentNullException("source");
            return source.Select(x => new FieldDefinition(x.Name, x.Name));
        }

        public static FieldManager ToFieldManager(this IEnumerable<FieldInformation> source) {
            if (source == null) throw new ArgumentNullException("source");
            FieldManager result = new FieldManager();
            List<FieldDefinition> fieldDefinitions = new List<FieldDefinition>();
            FieldDefinition field = null;
            foreach (FieldInformation item in source) {
                field = new FieldDefinition(item.Name, item.ViewModelName);
                if (item.HasExpression()) {
                    foreach (ExpressionValue expr in item.GetExpressions()) {
                        field.Add(expr.Expression);
                    }
                }
                result.Add(field);
            }
            return result;
        }
    }
}